package com.lagou.edu.controller;

import com.lagou.edu.service.CodeService;
import com.lagou.edu.service.EmailService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("code")
@RefreshScope
public class CodeController {

    @Reference
    private CodeService codeService;

    @Reference
    private EmailService emailService;

    @GetMapping("/create/{email}")
    @ResponseBody
    public Boolean createCode(@PathVariable("email") String email) {
        // 生成验证码，存储到数据库，并发送到邮箱
        int code = (int) ((Math.random() * 9 + 1) * 100000);
        System.out.println("code======="+code);

        String codeStr = String.valueOf(code);
        codeService.createCode(email,codeStr);
        // 调用邮件微服务发送邮件
        emailService.sendSimpleMail(email,"AuthCode",codeStr);
        return  true;
    }


    @GetMapping("/validate/{email}/{code}")
    public String validateCode(@PathVariable("email") String email,@PathVariable("code") String code) {
        return codeService.validateCode(email,code);
    }
}
